#include "commons.h"
#include "tasks.h"
#include "zxLib.h"

float odorLength = 1;
int zxAutoStop = 0;
int zxAutoSwitch = 0;

void callFunction(int n) {
    switch (n) {
            //zx's functions
        case 4311:
            setOdorLength(1.0); //Odor Length Always 1.0
            n = getFuncNumber(1, "BGo JGo MGo KGo");
            int m = getFuncNumber(1, "Stop Switch Go");
            zxGo_nogo(n, m, 20, 1, 0.5, 4, 0, 0);
            break;
        case 4411:
            n = getFuncNumber(1, "Stop Switch Go");
            setAutoStop(n == 1 ? 1 : 0);
            setAutoSwitch(n == 2 ? 1 : 0);
            setOdorLength(1.0); //Odor Length Always 1.0
            n = getFuncNumber(1, "1=DNMS 2=DMS");
            zxDNMSSessions(n == 1, 5.0, 10, 20, 0.5, 1.0, 100, 40);
            break;
        case 4412:
            splash("Inter Delay", "");
            setOdorLength(1.0); //Odor Length Always 1.0
            zxDNMSLaserSessions(laserInDelay, 5.0, 10, 20, 0.5, 50, 12, 1.0, 1.0, 4.0);
            break;
        case 4413:
            splash("During Puff", "");
            setOdorLength(1.0); //Odor Length Always 1.0
            zxDNMSLaserSessions(laserDuringOdor, 5.0, 10, 20, 0.5, 50, 12, 1.0, 1.0, 4.0);
            break;
    }
}

void setOdorLength(float in) {
    odorLength = in;
}

void shuffleArray(unsigned int orgArray[], unsigned int arraySize) {
    if (arraySize == 0 || arraySize == 1)
        return;

    int iter;
    for (iter = 0; iter < arraySize; iter++) {
        orgArray[iter] = iter;
    }
    int index, temp;
    for (iter = arraySize - 1; iter > 0; iter--) {
        index = rand() % (iter + 1);
        temp = orgArray[index];
        orgArray[index] = orgArray[iter];
        orgArray[iter] = temp;
    }
}

void LCDWriteNumber(int n, int digits, int x, int y) {
    if (digits < 1) {
        return;
    }
    LCD_set_xy(x, y);
    counts_dispose(n);
    if (digits > 2) {
        lcd_data(hunds);
    }
    if (digits > 1) {
        lcd_data(tens);
    }
    lcd_data(ones);
}

void LCDWriteChar(char ch, int x, int y) {
    LCD_set_xy(x, y);
    lcd_data(ch);
}

void zxDNMSSessions(int DNMS, float interOdorDelay, int interti, int trialNumperSess, float waterlen, float delayBeforeReward, int missLimit, int totalSession) {

    DelayNmSecNew(500);

    int currentTrial = 0;
    int currentSession = 0;
    correctRatio = 0;

    while ((currentMiss < missLimit) && (currentSession < totalSession)) {
        if (correctRatio >= 80 && zxAutoStop) {
            home_clr();
            LCDWriteString("Reached 80%");
            while (1);
        } else if (correctRatio >= 80 && zxAutoSwitch) {
            DNMS = DNMS ? 0 : 1;
        }
        currentSession++;
        localSendOnce(SpSessN, currentSession);
        localSendOnce(SpSess, 1);

        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        LCDWriteChar(DNMS ? 'N' : 'M', 1, 1);
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));
        LCDWriteNumber(currentSession, 2, 2, 2);

        //        timerCounterI = 0;
        //        while (timerCounterI < 3000) // inter session delay
        //        {
        //        }
        currentTrial = 0;
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        unsigned int shuffledList[4];
        while (currentTrial < (trialNumperSess) && currentMiss < missLimit) {
            shuffleArray(shuffledList, 4);
            int iter;
            for (iter = 0; iter < 4 && currentMiss < missLimit; iter++) {
                int firstOdor, secondOdor;
                int index = shuffledList[iter];
                firstOdor = (index == 0 || index == 2) ? odor_A : odor_B;
                secondOdor = (index == 1 || index == 2) ? odor_A : odor_B;
                LCD_set_xy(2, 1);
                lcd_data(firstOdor + 0x3F);
                lcd_data(secondOdor + 0x3F);
                zxDNMS(DNMS, firstOdor, odorLength, interOdorDelay, secondOdor, waterlen, interti, delayBeforeReward);
                currentTrial++;
            }
        }

        localSendOnce(SpSess, 0);
    }
    localSendOnce(SpTrain, 0);

}

void zxDNMSLaserSessions(int type, float thisdelay, int interti, int trialsPerSession, float WaterLen, int missLimit, int totalSession, float delay_before_reward, float delayBeforeLaser, float laserDuration) {
    localSendOnce(SpStepN, 0);
    DelayNmSecNew(2 * 1000);

    int currentTrial = 0;
    int currentSession = 0;

    while ((currentMiss < missLimit) && (currentSession < totalSession)) {
        currentSession++;
        localSendOnce(SpOdorDelay, thisdelay);
        localSendOnce(SpSess, 1);

        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));

        LCDWriteNumber(currentSession, 2, 2, 2);
        currentTrial = 0;
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;
        unsigned int shuffledList[4];
        while (currentTrial < trialsPerSession && currentMiss < missLimit) {
            shuffleArray(shuffledList, 4);
            int iter;
            for (iter = 0; iter < 4 && currentMiss < missLimit; iter++) {
                DelayNmSecNew(1000);
                int firstOdor, secondOdor;
                int index = shuffledList[iter];
                firstOdor = (index == 0 || index == 2) ? odor_A : odor_B;
                secondOdor = (index == 1 || index == 2) ? odor_A : odor_B;
                LCD_set_xy(1, 1);
                lcd_data(firstOdor + 0x3F);
                lcd_data(secondOdor + 0x3F);
                zxDNMSLaserTrial(type, firstOdor, odorLength, thisdelay, secondOdor, WaterLen, interti, 0, delay_before_reward, delayBeforeLaser, laserDuration, currentTrial);
                currentTrial++;
            }
        }
        localSendOnce(SpSess, 0);
    }
    localSendOnce(SpTrain, 0); // send it's the end
}

void zxDNMS(int DNMS, int FirstOdorIn, float odorLength, float OdorDelayIn, int SecondOdorIn, float WaterLIn, int IntervalIn, float delay_before_reward) {
    localSendOnce(FirstOdorIn + 7, 1); // odor_A=2, spOdor_A=9
    LCDWriteChar('1', 4, 1);
    Valve_ON(FirstOdorIn, period * 2);
    timerCounterI = 0;
    while (timerCounterI < odorLength * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            DelayNmSecNew(20);
        }
    }
    Valve_OFF(FirstOdorIn);
    localSendOnce(FirstOdorIn + 7, 0); // 0 for close
    LCDWriteChar('d', 4, 1);
    timerCounterI = 0;
    while (timerCounterI < OdorDelayIn * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            DelayNmSecNew(20);
        }
    }
    LCDWriteChar('2', 4, 1);
    localSendOnce(SecondOdorIn + 7, 1); // 1 for open
    Valve_ON(SecondOdorIn, period * 2);
    timerCounterI = 0;
    while (timerCounterI < odorLength * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            DelayNmSecNew(20);

        }
    }
    Valve_OFF(SecondOdorIn);
    localSendOnce(SecondOdorIn + 7, 0); // 0 for close
    LCDWriteChar('D', 4, 1);
    timerCounterI = 0;
    while (timerCounterI < delay_before_reward * 1000) // the delay after second odor
    {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            DelayNmSecNew(20);
        }
    }

    LCDWriteChar('R', 4, 1);
    Flags.lick = 0;
    timerCounterI = 0;
    while (timerCounterI < WaterLIn * 1000) {
        if (LickOnPin) {
            Flags.lick = 1;
            localSendOnce(SpLick, 1);
            if ((FirstOdorIn != SecondOdorIn && DNMS) || (FirstOdorIn == SecondOdorIn && !DNMS)) {
                Valve_ON(water_sweet, period * 2);
                localSendOnce(SpWater_sweet, 1);
            }
            DelayNmSecNew(20);
        }
    }
    Valve_OFF(water_sweet);

    if (Flags.lick) {
        Flags.lick = 0;
        if ((FirstOdorIn != SecondOdorIn && DNMS) || (FirstOdorIn == SecondOdorIn && !DNMS)) {
            localSendOnce(SpWater_sweet, 0);
            hit++;
            localSendOnce(SpHit, 1);
            LCDWriteNumber((int) hit, 3, 6, 1);
        } else {
            falseAlarm++;
            localSendOnce(SpFalseAlarm, 1);
            LCDWriteNumber((int) falseAlarm, 3, 6, 2);
        }
    } else {
        if ((FirstOdorIn != SecondOdorIn && DNMS) || (FirstOdorIn == SecondOdorIn && !DNMS)) {
            miss++;
            localSendOnce(SpMiss, 1);
            LCDWriteNumber((int) miss, 3, 10, 1);
        } else {
            correctRejection++;
            localSendOnce(SpCorrectRejection, 1);
            LCDWriteNumber((int) correctRejection, 3, 10, 2);
        }
    }
    int totalTrials = hit + correctRejection + miss + falseAlarm;
    LCDWriteNumber(totalTrials, 3, 14, 2);
    if (hit + correctRejection > 0) {
        correctRatio = 100 * (hit + correctRejection) / totalTrials;
        LCDWriteNumber(correctRatio, 2, 14, 1);
    }

    LCDWriteChar('I', 4, 1);
    localSendOnce(SpITI, 1);
    timerCounterI = 0;
    while (timerCounterI < IntervalIn * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            DelayNmSecNew(20);
        }
    }
    localSendOnce(SpITI, 0);
}

void zxDNMSLaserTrial(int type, int FirstOdorIn, float odorLength, float OdorDelayIn, int SecondOdorIn, float WaterLIn, int IntervalIn, int prolong, float delay_before_reward, float delayBeforeLaser, float laserDuration, int currentTrial) {
    localSendOnce(Splaser, (currentTrial % 2 == 0) ? 0 : 1);
    int stim = currentTrial % 2;
    Valve_ON(FirstOdorIn, period * 2);
    localSendOnce(FirstOdorIn + 7, 1);
    if (type == laserDuringOdor && stim) {
        LCD_set_xy(3, 1);
        lcd_data('L');
        zxLaser(laserFreq, durationPerLaserStim, odorLength * 1000);
        LCD_set_xy(3, 1);
        lcd_data('.');
    } else {
        timerCounterI = 0;
        while (timerCounterI < (odorLength)*1000) {
            if (LickOnPin) localSendOnce(SpLick, 1);
        }
    }
    Valve_OFF(FirstOdorIn);
    localSendOnce(FirstOdorIn + 7, 0);

    ///////////-inter odor interval-/////////////////
    if (!(type == laserInDelay && stim)) {
        timerCounterI = 0;
        while (timerCounterI < OdorDelayIn * 1000) {
            if (LickOnPin) localSendOnce(SpLick, 1);
        }
    } else {
        LCD_set_xy(3, 1);
        lcd_data('L');
        timerCounterI = 0;
        while (timerCounterI < delayBeforeLaser * 1000) {
            if (LickOnPin) localSendOnce(SpLick, 1);
        }

        zxLaser(laserFreq, durationPerLaserStim, laserDuration * 1000);

        timerCounterI = 0;
        while (timerCounterI < (OdorDelayIn - delayBeforeLaser - laserDuration)*1000) {
            if (LickOnPin) localSendOnce(SpLick, 1);
        }
        LCD_set_xy(3, 1);
        lcd_data('.');
    }

    ///////////-Second odor-/////////////////

    Valve_ON(SecondOdorIn, period * 2);
    //    if (SecondOdorIn == odor_A) Out1 = 1;
    //    else if (SecondOdorIn == odor_B) Out2 = 1;

    localSendOnce(SecondOdorIn + 7, 1);
    if (type == laserDuringOdor && stim) {
        LCD_set_xy(3, 1);
        lcd_data('L');
        zxLaser(laserFreq, durationPerLaserStim, odorLength * 1000);
        LCD_set_xy(3, 1);
        lcd_data('.');
    } else {
        timerCounterI = 0;
        while (timerCounterI < (odorLength)*1000) {
            if (LickOnPin) localSendOnce(SpLick, 1);
        }
    }
    Valve_OFF(SecondOdorIn);

    //    if (SecondOdorIn == odor_A) Out1 = 0;
    //    else if (SecondOdorIn == odor_B) Out2 = 0;

    localSendOnce(SecondOdorIn + 7, 0);
    ////////-delay before reward-///////

    timerCounterI = 0;
    while (timerCounterI < delay_before_reward * 1000) // the delay after second odor
    {
        if (LickOnPin) localSendOnce(SpLick, 1);
    }

    if (FirstOdorIn != SecondOdorIn) {
        ///////////-reward-/////////////////
        Flags.lick = 0;

        timerCounterI = 0;
        while (timerCounterI < WaterLIn * 1000) {
            if (LickOnPin) {
                localSendOnce(SpLick, 1);
                Valve_ON(water_sweet, period * 2);
                localSendOnce(SpWater_sweet, 1);
                Flags.lick = 1;
            }
        }
        Valve_OFF(water_sweet);
        localSendOnce(SpWater_sweet, 0);

        if (Flags.lick) {
            Flags.lick = 0;
            currentMiss = 0;
            hit++;
            localSendOnce(SpHit, 1);
            LCDWriteNumber(hit, 3, 6, 1);
        } else {
            currentMiss++;
            miss++;
            localSendOnce(SpMiss, 1);
            LCDWriteNumber(miss, 3, 10, 1);
        }
    } else {
        ///////////- punishment-/////////////////
        Flags.lick = 0;
        timerCounterI = 0;
        while (timerCounterI < WaterLIn * 1000) {
            if (LickOnPin) {
                localSendOnce(SpLick, 1);
                Flags.lick = 1;
            }
        }
        if (Flags.lick) {
            timerCounterI = 0;
            while (timerCounterI < prolong * 1000) { // longer ITI

            }
            Flags.lick = 0; // Reset flag
            falseAlarm++;
            currentMiss = 0;
            localSendOnce(SpFalseAlarm, 1);
            LCDWriteNumber(falseAlarm, 3, 6, 2);
        } else {
            correctRejection++;
            Flags.lick = 0;
            localSendOnce(SpCorrectRejection, 1);
            LCDWriteNumber(correctRejection, 3, 10, 2);
        }
    }
    // Total Trials
    int totalTrials = hit + correctRejection + miss + falseAlarm;
    LCDWriteNumber(totalTrials, 3, 14, 2);
    // Discrimination rate
    if (hit + correctRejection > 0) {
        correctRatio = 100 * (hit + correctRejection) / totalTrials;
        LCDWriteNumber(correctRatio, 2, 14, 1);
    }

    ///--ITI1---///
    timerCounterI = 0;
    while (timerCounterI < IntervalIn * 1000) {
        if (LickOnPin) localSendOnce(SpLick, 1);
    }
}

void zxLaser(int FreqStim, int DuraStim, float totalStimTime) {
    int Interval = (1000 / FreqStim) - DuraStim;
    int ind;
    int NumSpikeBurst = (totalStimTime / 1000) * FreqStim;

    for (ind = 1; ind <= NumSpikeBurst; ind++) {

        Out4 = 1;

        // delay for DuraStim
        timerCounterI = 0;
        while (timerCounterI < DuraStim) {
            if (LickOnPin) localSendOnce(SpLick, 1);
        }

        Out4 = 0;

        // delay for Interval
        timerCounterI = 0;
        while (timerCounterI < Interval) {
            if (LickOnPin) localSendOnce(SpLick, 1);
        }
    }
}

void zxGo_nogo(int odorType, int autoSwitch, int trialInSession, int delay, float waterlen,                                                                                                                  \
             int ITI, int odorlaseryes, int delaylaseryes) {

    int go_odor;
    int nogo_odor;
    switch (odorType) {
        case 1:
            go_odor = odor_A;
            nogo_odor = odor_B;
            break;
        case 2:
            go_odor = odor_B;
            nogo_odor = odor_A;
            break;
        case 3:
            go_odor = odor_C;
            nogo_odor = odor_D;
            break;
        case 4:
            go_odor = odor_D;
            nogo_odor = odor_C;
            break;
        default:
            go_odor = odor_A;
            nogo_odor = odor_B;
    }

    DelayNmSecNew(500);

    int sessionNum = 0;
    int lastRatio = 0;
    unsigned int shuffledList[trialInSession];
    while (1) { //while (1)  no stop
        if ((correctRatio >= HighThres) || (correctRatio >= lowThres && lastRatio >= lowThres)) {
            if (autoSwitch == 2) {
                int temp = nogo_odor;
                nogo_odor = go_odor;
                go_odor = temp;
            } else if (autoSwitch == 1) {
                splash("Reach Criteria", "");
                while (1) {
                }
            }
        }
        shuffleArray(shuffledList, trialInSession);
        lastRatio = correctRatio;
        hit = 0;
        miss = 0;
        falseAlarm = 0;
        correctRejection = 0;

        sessionNum++;
        localSendOnce(SpSess, 1);
        home_clr();
        puts_lcd((unsigned char*) &text7[0], sizeof (text7));
        line_2();
        puts_lcd((unsigned char*) &text8[0], sizeof (text8));

        LCD_set_xy(1, 1);
        switch (go_odor) {
            case odor_A:
                LCDWriteString("Bgo");
                break;
            case odor_B:
                LCDWriteString("Jgo");
                break;
            case odor_C:
                LCDWriteString("Mgo");
                break;
            case odor_D:
                LCDWriteString("Kgo");
                break;
        }

        LCDWriteNumber(sessionNum, 2, 2, 2);
        int currentTrial;
        int laserOn;
        for (currentTrial = 0; currentTrial < trialInSession; currentTrial++) {
            laserOn = !(currentTrial % 2 == 0);
            int index = shuffledList[currentTrial];
            if (index % 2 == 0) {
                zxGoNogoTrial(go_odor, 1, laserOn, odorLength, delay, waterlen, ITI, odorlaseryes, delaylaseryes);
            } else {
                zxGoNogoTrial(nogo_odor, 0, laserOn, odorLength, delay, waterlen, ITI, odorlaseryes, delaylaseryes);
            }
        }
        localSendOnce(SpSess, 0);
    }
}

void zxGoNogoTrial(int odor, int isGo, int laserOn, float odorlen, int delay, float waterlen,                                                                                                                  \
             int ITI, int laserDuringPuff, int laserDuringDelay) {
    if (laserOn) {
        LCDWriteChar('L', 3, 1);
        localSendOnce(SpWater_sweet, 1);
        zxLaser(laserFreq, durationPerLaserStim, 4000);
        LCDWriteChar('o', 3, 1);
    } else {
        waitReportLick(4000);
    }


    Valve_ON(odor, period * 2);
    //    if (odor == odor_A) localSendOnce(SpOdor_A, 1);
    //    else if (odor == odor_B) localSendOnce(SpOdor_B, 1);

    localSendOnce(odor == odor_A || odor == odor_C ? SpOdor_A : SpOdor_B, 1);

    //    if (laserDuringPuff == 1) {
    //        Out4 = 1;
    //        timerCounterI = 0;
    //        while (timerCounterI < odorlen * 1000) {
    //            if (LickOnPin) localSendOnce(SpLick, 1);
    //        }
    //        Out4 = 0;
    //    } else {
    waitReportLick(odorlen * 1000);
    //    }
    Valve_OFF(odor);
    //---delay--//
    //    if (laserDuringDelay == 1) {
    //        Out4 = 1;
    //        timerCounterI = 0;
    //        while (timerCounterI < delay * 1000) {
    //            if (LickOnPin) localSendOnce(SpLick, 1);
    //        }
    //        Out4 = 0;
    //    } else {
    waitReportLick(delay * 1000);
    //    }

    //---water--//
    Flags.lick = 0;
    timerCounterI = 0;
    while (timerCounterI < waterlen * 1000) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            if (isGo) {
                Valve_ON(water_sweet, period * 2);
                //                localSendOnce(SpWater_sweet, 1);
            }
            Flags.lick = 1;
        }
    }

    Valve_OFF(water_sweet);
    if (Flags.lick && isGo) {
        currentMiss = 0;
        hit++;
        localSendOnce(SpHit, 1);
        LCDWriteNumber(hit, 3, 6, 1);
    } else if (isGo && !Flags.lick) {
        currentMiss++;
        miss++;
        localSendOnce(SpMiss, 1);
        LCDWriteNumber(miss, 3, 10, 1);
    } else if ((!isGo) && Flags.lick) {
        falseAlarm++;
        localSendOnce(SpFalseAlarm, 1);
        LCDWriteNumber(falseAlarm, 3, 6, 2);
    } else if ((!isGo) && !Flags.lick) {
        correctRejection++;
        localSendOnce(SpCorrectRejection, 1);
        LCDWriteNumber(correctRejection, 3, 10, 2);
    }
    Flags.lick = 0;
    // Total Trials
    int totalTrials = hit + correctRejection + miss + falseAlarm;
    LCDWriteNumber(totalTrials, 3, 14, 2);

    // Discrimination rate
    if (hit + correctRejection > 0) {
        correctRatio = 100 * (hit + correctRejection) / totalTrials;
        LCDWriteNumber(correctRatio, 2, 14, 1);
    }
    //--iti--//

    waitReportLick(ITI * 1000);
}

void odorDepeltion(int totalTrial) {
    int n = getFuncNumber(1, "1=BJ 2=MK 3=All");

    int i = 0;
    while (i < totalTrial) {
        home_clr();
        LCDWriteString("Odor Depletion");
        int percent = i > 599 ? 999 : i * 100 / 60;
        LCDWriteNumber(percent, 3, 1, 2);
        LCDWriteString("%, Valve ");
        if (n != 2) {
            LCD_set_xy(15, 2);
            lcd_data(0x31);
            Valve_ON(odor_A, period * 2);
            DelayNmSecNew(15000);
            Valve_OFF(odor_A);
            LCD_set_xy(15, 2);
            lcd_data(0x32);
            Valve_ON(odor_B, period * 2);
            DelayNmSecNew(15000);
            Valve_OFF(odor_B);
        }
        if (n != 1) {
            LCD_set_xy(15, 2);
            lcd_data(0x33);
            Valve_ON(5, period * 2);
            DelayNmSecNew(15000);
            Valve_OFF(5);
            LCD_set_xy(15, 2);
            lcd_data(0x34);
            Valve_ON(6, period * 2);
            DelayNmSecNew(15000);
            Valve_OFF(6);
        }
        i++;
    }
}

void feedWater(int threshold) {
    int topTime = 600;
    switch (threshold) {
        case 1:
            topTime = 2;
            break;
        case 2:
            topTime = 5;
            break;
        case 3:
            topTime = 10;
            break;
        case 4:
            topTime = 30;
            break;
        case 5:
            topTime = 600;
    }


    home_clr();
    LCDWriteString("Auto Feed Water");
    Valve_ON(water_sweet, period * 2);
    int secondsWithoutLick = 0;
    int licked = 0;
    while (secondsWithoutLick < topTime) {
        LCDWriteNumber(topTime - secondsWithoutLick, 3, 1, 2);
        timerCounterI = 0;
        while (timerCounterI < 1000) {
            if (LickOnPin) {
                licked = 1;
            }
        }
        secondsWithoutLick = licked ? 0 : secondsWithoutLick + 1;
        licked = 0;
    }
    Valve_OFF(water_sweet);
    home_clr();
    LCDWriteString("Licking Stopped");
    while (1);
}

void setAutoStop(int autoStop_In) {
    zxAutoStop = autoStop_In;
}

void setAutoSwitch(int autoSwitch_in) {
    zxAutoSwitch = autoSwitch_in;
}

void splash(char * s1, char * s2) {
    home_clr();
    LCDWriteString(s1);
    line_2();
    LCDWriteString(s2);
    DelayNmSecNew(1000);
}

void waitReportLick(int time) {
    timerCounterI = 0;
    while (timerCounterI < time) {
        if (LickOnPin) {
            localSendOnce(SpLick, 1);
            DelayNmSecNew(20);
        }
    }
}

